! Created by ZhaoYH on 2023/10/20.
!! Author: Zhao Yuhui
!! Date: 2023/10/20
!! Note:

module DataWriter
    integer, public, save :: wd_xdef
    integer, public, save :: wd_ydef
    integer, public, save :: wd_tdef
    integer, public, save :: wd_zdef
    integer, public, save :: wd_argc
    public write_data

contains
    subroutine write_data(wd_path, wd_array)
        implicit none
        character(len=128), intent(in) :: wd_path
        real, intent(in) :: wd_array(:, :, :, :, :)

        integer :: i, j, k, time, arg
        integer :: irec = 1

        open(unit=20, file=wd_path, form='unformatted', status='unknown', access='direct', &
                recl=wd_xdef * wd_ydef * wd_zdef * 4)

        do time = 1, wd_tdef
            do arg = 1, wd_argc
                write(unit=20, rec=irec) (((wd_array(arg,i,j,time,k),i=1,wd_xdef),j=1,wd_ydef),k=1,wd_zdef)
                irec = irec + 1
            end do
        end do

        close(20)

        return

    end subroutine write_data

end module DataWriter